﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Collections;
using StateGrid.MDBservice;
using System.IO;
using iTextSharp.text.pdf;

namespace StateGrid.ZCWX
{
    class ZCWX_MDB : MDBservice.MDBservice
    {
        private ZCWX_DAO zcwxDAO = new ZCWX_DAO();

        public override void clear()
        {
            base.clear();
        }

        public override String getTest()
        {
            IList<ZCWX_FL> r = zcwxDAO.GetAllZCWX_FL();
            String ret = String.Empty;
            foreach (ZCWX_FL i in r)
            {
                ret += i.ToString();
            }
            return ret;
        }

        public ZCWX_MDB()
        {
            pmTableName = "ZCWX_WF";
            formatPmTableName = "ZCWX";
            title = "自产文献";
        }

        public override void setColumnList()
        {
            this.ColumnList = new List<Column>();
            if (hasFormatTable)
            {
                this.ColumnList.Add(new Column("分类编码", OleDbType.WChar, 10));
                this.ColumnList.Add(new Column("索引编码", OleDbType.WChar, 30));
                this.ColumnList.Add(new Column("馆藏号", OleDbType.WChar, 30));
                this.ColumnList.Add(new Column("中文篇名", OleDbType.WChar, 100));
                this.ColumnList.Add(new Column("出版单位", OleDbType.WChar, 50));
                this.ColumnList.Add(new Column("文献密级", OleDbType.WChar, 4));
                this.ColumnList.Add(new Column("文献类型", OleDbType.WChar, 4));
                this.ColumnList.Add(new Column("责任个人", OleDbType.WChar, 50));
                this.ColumnList.Add(new Column("责任单位", OleDbType.WChar, 50));
                this.ColumnList.Add(new Column("出版日期", OleDbType.Date));
                this.ColumnList.Add(new Column("载体形态", OleDbType.WChar, 20));
                this.ColumnList.Add(new Column("国图分类", OleDbType.WChar, 30));
                this.ColumnList.Add(new Column("中文主题词", OleDbType.WChar, 255));
                this.ColumnList.Add(new Column("中文文摘", OleDbType.WChar, 0));
                this.ColumnList.Add(new Column("备注", OleDbType.WChar, 255));
                this.ColumnList.Add(new Column("更新日期", OleDbType.Date));
            }
            else
            {
                //未支持
            }
        }

        protected override PM getPM(OleDbDataReader reader, out String err)
        {
            err = String.Empty;
            ZCWX_PM pm = new ZCWX_PM();
            if (hasFormatTable)
            {
                pm.flbm = reader["分类编码"].ToString().Trim();
                pm.sybm = reader["索引编码"].ToString().Trim();
                pm.gch = reader["馆藏号"].ToString().Trim();
                pm.zwpm = reader["中文篇名"].ToString().Trim();
                pm.cbdw = reader["出版单位"].ToString().Trim();
                pm.wxmj = reader["文献密级"].ToString().Trim();
                pm.wxlx = reader["文献类型"].ToString().Trim();
                pm.zrgr = reader["责任个人"].ToString().Trim();
                pm.zrdw = reader["责任单位"].ToString().Trim();
                try
                {
                    if (String.Empty.Equals(reader["出版日期"].ToString().Trim()))
                    {
                        //err += pm.flbm + "-" + pm.zwpm + ":" + pm.cbdw + ":出版日期为空\n";
                        pm.cbrq = null;
                    }
                    else
                        pm.cbrq = DateTime.Parse(reader["出版日期"].ToString().Trim());
                }
                catch
                {
                    err += pm.flbm + ":" + pm.zwpm + ":" + pm.cbdw + ": 出版日期\"" + reader["出版日期"] + "\"出错\n";
                }
                pm.ztxt = reader["载体形态"].ToString().Trim();
                pm.gtfl = reader["国图分类"].ToString().Trim();
                pm.zwztc = reader["中文主题词"].ToString().Trim();
                pm.zwwz = reader["中文文摘"].ToString().Trim();
                pm.bz = reader["备注"].ToString().Trim();
            }
            else
            {
                //不支持
            }
            pm.dlr = user.name;
            pm.fillEmptyStringAsNull();
            return pm;
        }

        public override bool checkPMValid(PM pm, out string errMsg)
        {
            errMsg = String.Empty;
            ZCWX_PM zcwx = (ZCWX_PM)pm;
            this.openOleDbConnection();
            sqlcmd.CommandText = @"select * from " + formatPmTableName + " where 分类编码=@分类编码 and 中文篇名=@中文篇名 and 出版单位=@出版单位 and 出版日期=@出版日期";
            sqlcmd.Connection = this.ObjConnection;
            sqlcmd.Parameters.Add(new OleDbParameter("分类编码", zcwx.flbm));
            sqlcmd.Parameters.Add(new OleDbParameter("中文篇名", zcwx.zwpm));
            sqlcmd.Parameters.Add(new OleDbParameter("出版单位", zcwx.cbdw));
            OleDbParameter parameter;
            parameter = new OleDbParameter("出版日期", OleDbType.DBDate);
            parameter.Value = zcwx.cbrq;
            sqlcmd.Parameters.Add(parameter);
            OleDbDataReader reader = sqlcmd.ExecuteReader();

            if (reader.Read())
            {
                reader.Close();
                sqlcmd.Parameters.Clear();
                return true;
            }
            else
            {
                reader.Close();
                sqlcmd.Parameters.Clear();
                return false;
            }
            //            this.closeOleDbConnection();
        }

        public override string formatData(PM pm)
        {
            //反馈信息
            String result = String.Empty;
            String Empty = "NULL";
            ZCWX_PM zcwx = (ZCWX_PM)pm;

            zcwx.zrgr = GlobalMethod.Semicolon(zcwx.zrgr);
            zcwx.gtfl = GlobalMethod.Semicolon(zcwx.gtfl);
            zcwx.zwztc = GlobalMethod.Semicolon(zcwx.zwztc);

            zcwx.dlr = user.name;
            zcwx.gxrq = DateTime.Now;
            ZCWX_FL fl = zcwxDAO.GetZCWX_FLByFlbm(zcwx.flbm);
            if (fl == null)
            {
                result += "分类编码" + zcwx.flbm + "不存在 ";
            }
            else
            {
                //zcwx.gch = fl.dwdm;   //待确认
                zcwx.sybm = fl.dwdm + "-" + zcwx.cbrq.Value.Year.ToString() + "-";
            }
            if (Empty.Equals(zcwx.zwpm))
            {
                result += "标题为空 ";
            }

            if (!result.Equals(String.Empty))
            {
                result += " : " + zcwx.zrgr + "《" + zcwx.zwpm + "》\n";
            }

            return result;
        }

        public override String insertPM(PM pm)
        {
            ZCWX_PM zcwx = (ZCWX_PM)pm;
            int back;
            String err = String.Empty;
            //打开连接
            this.openOleDbConnection();
            OleDbCommand sqlcmd = new OleDbCommand();
            if (!String.Empty.Equals(validateTableName(formatPmTableName)))
            {
                sqlcmd.CommandText = @"create Table " + formatPmTableName + @"(分类编码 char(10), 索引编码 char(30), 馆藏号 char(30), 中文篇名 char(100), 出版单位 char(50), 文献密级 char(4), 文献类型 char(4), 责任个人 char(50), 责任单位 char(50), 出版日期 datetime, 载体形态 char(20), 国图分类 char(30), 中文主题词 char(255), 中文文摘 text, 备注 char(255), 更新日期 datetime)";
                sqlcmd.Connection = this.ObjConnection;
                try
                {
                    back = sqlcmd.ExecuteNonQuery();
                }
                catch (Exception e)
                {
                    err = e.Message + "\n";
                    return err;
                }
            }

            sqlcmd.CommandText = @"insert into " + formatPmTableName + @"(分类编码,索引编码,馆藏号,中文篇名,出版单位,文献密级,文献类型,责任个人,责任单位,出版日期,载体形态,国图分类,中文主题词,中文文摘,备注,更新日期) values(@分类编码,@索引编码,@馆藏号,@中文篇名,@出版单位,@文献密级,@文献类型,@责任个人,@责任单位,@出版日期,@载体形态,@国图分类,@中文主题词,@中文文摘,@备注,@更新日期)";
            sqlcmd.Connection = this.ObjConnection;
            OleDbParameter parameter;
            sqlcmd.Parameters.Add(new OleDbParameter("分类编码", zcwx.flbm));
            sqlcmd.Parameters.Add(new OleDbParameter("索引编码", zcwx.sybm));
            sqlcmd.Parameters.Add(new OleDbParameter("馆藏号", zcwx.gch));
            sqlcmd.Parameters.Add(new OleDbParameter("中文篇名", zcwx.zwpm));
            sqlcmd.Parameters.Add(new OleDbParameter("出版单位", zcwx.cbdw));
            sqlcmd.Parameters.Add(new OleDbParameter("文献密级", zcwx.wxmj));
            sqlcmd.Parameters.Add(new OleDbParameter("文献类型", zcwx.wxlx));
            sqlcmd.Parameters.Add(new OleDbParameter("责任个人", zcwx.zrgr));
            sqlcmd.Parameters.Add(new OleDbParameter("责任单位", zcwx.zrdw));
            parameter = new OleDbParameter("出版日期", OleDbType.DBDate);
            parameter.Value = zcwx.cbrq;
            sqlcmd.Parameters.Add(parameter);
            sqlcmd.Parameters.Add(new OleDbParameter("载体形态", zcwx.ztxt));
            sqlcmd.Parameters.Add(new OleDbParameter("国图分类", zcwx.gtfl));
            sqlcmd.Parameters.Add(new OleDbParameter("中文主题词", zcwx.zwztc));
            sqlcmd.Parameters.Add(new OleDbParameter("中文文摘", zcwx.zwwz));
            sqlcmd.Parameters.Add(new OleDbParameter("备注", zcwx.bz));
            parameter = new OleDbParameter("更新日期", OleDbType.DBDate);
            parameter.Value = zcwx.gxrq;
            sqlcmd.Parameters.Add(parameter);
            try
            {
                back = sqlcmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                err = e.Message + "\n";
            }
            sqlcmd.Parameters.Clear();

            this.closeOleDbConnection();
            return err;
        }

        public override int deletePM(PM pm, out String err)
        {
            int back = 0;
            err = String.Empty;
            ZCWX_PM zcwx = (ZCWX_PM)pm;
            //打开连接
            this.openOleDbConnection();
            OleDbCommand sqlcmd = new OleDbCommand();
            if (!String.Empty.Equals(validateTableName(formatPmTableName)))
            {
                sqlcmd.CommandText = @"create Table " + formatPmTableName + @"(分类编码 char(20), 标题 char(255), 作者 char(150), 日期 datetime, 版号 smallint, 栏目 char(100), 正文 text)";
                sqlcmd.Connection = this.ObjConnection;
                try
                {
                    back = sqlcmd.ExecuteNonQuery();
                }
                catch (Exception e)
                {
                    err = e.Message + "\n";
                    return 0;
                }
            }
            //sql语句
            sqlcmd.CommandText = @"delete from " + formatPmTableName + " where 分类编码=@分类编码 and 中文篇名=@中文篇名 and 出版单位=@出版单位 and 出版日期=@出版日期";
            sqlcmd.Connection = this.ObjConnection;
            sqlcmd.Parameters.Add(new OleDbParameter("分类编码", zcwx.flbm));
            sqlcmd.Parameters.Add(new OleDbParameter("中文篇名", zcwx.zwpm));
            sqlcmd.Parameters.Add(new OleDbParameter("出版单位", zcwx.cbdw));
            OleDbParameter parameter;
            parameter = new OleDbParameter("出版日期", OleDbType.DBDate);
            parameter.Value = zcwx.cbrq;
            sqlcmd.Parameters.Add(parameter);

            try
            {
                back = sqlcmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                err = "Exception: " + e.Message + "\n";
            }
            sqlcmd.Parameters.Clear();
            //            this.closeOleDbConnection();
            return back;
        }
        /* 现在的方法先delete再insert，就不用为每文库写update了
        public override bool updatePM(PM pm)
        {
            ZWQK_PM zwqk = (ZWQK_PM)pm;
            //打开连接
            this.openOleDbConnection();

            //sql语句
            OleDbCommand sqlcmd = new OleDbCommand(@"update ZWQK set 作者=@作者,作者单位=@作者单位,中文主题词=@中文主题词,外文主题词=@外文主题词,国图分类=@国图分类 where 中文篇名=@中文篇名 and 文件名=@文件名", this.ObjConnection);

            sqlcmd.Parameters.Add(new OleDbParameter("作者", zwqk.zz));
            sqlcmd.Parameters.Add(new OleDbParameter("作者单位", zwqk.zzdw));
            sqlcmd.Parameters.Add(new OleDbParameter("中文主题词", zwqk.zwztc));
            sqlcmd.Parameters.Add(new OleDbParameter("外文主题词", zwqk.wwztc));
            sqlcmd.Parameters.Add(new OleDbParameter("国图分类", zwqk.gtfl));
            sqlcmd.Parameters.Add(new OleDbParameter("中文篇名", zwqk.zwpm));
            sqlcmd.Parameters.Add(new OleDbParameter("文件名", zwqk.wjm));
            int back = sqlcmd.ExecuteNonQuery();
            if (back == 0)
            {
                return false;
            }
            //objConnection.Close();
            return true;
        }*/

        public override bool findServerDuplicate(PM pm)
        {
            ZCWX_PM zcwx = (ZCWX_PM)pm;
            ZCWX_PM result = zcwxDAO.GetZCWX_PMByZwpmAndCbdw(zcwx.zwpm, zcwx.cbdw);
            if (result != null)
                return true;
            else
                return false;
        }

        public override String insertPMwithUpdateWJM(PM pm)
        {
            String err = String.Empty;
            ZCWX_PM zcwx = (ZCWX_PM)pm;
            ZCWX_PM maxPm;
            // 自动编码“索引编码”的流水号
            int index = 0;
            try
            {
                maxPm = zcwxDAO.getMaxZCWX_PM(zcwx.flbm, zcwx.cbrq.Value.Year.ToString());
            }
            catch (Exception e)
            {
                err += e.Message;
                maxPm = null;
                return err;
            }
            if (maxPm != null)
            {
                int pos = maxPm.sybm.LastIndexOf('-');
                index = int.Parse(maxPm.sybm.Substring(pos + 1)) + 1;
            }
            String serialStr = "0000" + index.ToString();
            zcwx.sybm += serialStr.Substring(serialStr.Length - 4);
            try
            {
                zcwxDAO.InsertZCWX_PM(zcwx);
            }
            catch (Exception e)
            {
                err += e.Message + "\n";
            }
            return err;
        }

        public override string addOneRecord(PM pm)
        {
            String err = String.Empty;
            ZCWX_PM zcwx = (ZCWX_PM)pm;
            zcwx.gxrq = DateTime.Now;
            zcwx.eraseNullInEmptyString();
            err = insertPMwithUpdateWJM(pm);
            return err;
        }

    }
}
